var tabIndex = 0;
var isreload = true;

// 今天
var date = new Date();
var hour = date.getHours();
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
var _month = (month + 1) < 10 ? "0" + (month + 1) : month + 1;
var _day = day < 10 ? "0" + day : day;
var today = year + "-" + _month + "-" + _day;
var showToday = _month + "月" + _day + "日";
newday = day;
var day1;

// 明天
date = new Date(year, month, day + 1);
year = date.getFullYear();
month = date.getMonth();
day = date.getDate();
_month = (month + 1) < 10 ? "0" + (month + 1) : month + 1;
_day = day < 10 ? "0" + day : day;

var tomorrow = year + "-" + _month + "-" + _day;
var showTomorrow = _month + "月" + _day + "日";

var args = {date : today};
var param = {reqData : args, reqUrl : "queryTime"};

var movie_a = {movie0 : false};
var time_a = {all : false, am : false, pm : false, night : false};
var edition_a = {all : false, imax : false, d2 : false, d3 : false};

$(function()
{
	$("#divToday").attr("title", "今日上映&nbsp;" + showToday);
	$("#divTomorrow").attr("title", "明日上映&nbsp;" + showTomorrow);
	
	$("#divTabs").tabs(
	{
		plain : true,
		narrow : true
	});
	
	// easyui tabs事件
	$('#divTabs').tabs(
	{
	    onSelect : tabSelect
	});
	
	queryTime();
});

//Tab切换
tabSelect = function(title, index)
{
	day1 =  parseInt(day);
	isreload = true;
	resetData();
	resetCss();

	tabIndex = index;
	
	if(tabIndex == 0)
	{
		args.date = today;
	}
	else
	{
		args.date = tomorrow;
	}

	queryTime();
}

queryTime = function()
{
	ajaxRequest(param, function(returnData)
	{
		if(isreload && returnData.movie)
		{
			var data = returnData.movie;
			var movieArr = [];
			var playIds = new Array();
			var index = 1;
			
			for(var key in data)
			{
				var id = "movie" + index;
				var values = data[key];
				
				$(values).each(function()
				{
					playIds.push(this);
				});

				var a = "<a id=\"" + id + "\" vals=\"" + values + "\" onclick=\"setMovieCss(this)\">" + key + "</a>";
				movieArr.push(a);
				
				movie_a[id] = false;
				
				index ++;
			}

			var all = "<a id=\"movie0\" vals=\"" + playIds.join(",") + "\" onclick=\"setMovieCss(this)\">[全部]</a>";
			
			movieArr.splice(0, 0, all);

			$("#spanMovie" + tabIndex).html(movieArr.join("&nbsp;"));
			
			isreload = false;
		}
		
		if(returnData.play)
		{
			$("#divTable" + tabIndex + " table tr").empty();

			var data = returnData.play;
			
			var trs = [];
			
			var preMovieName = "";
			

			var newday1 = parseInt(newday);

			$(data).each(function()
			{
				var newtime = parseInt(this.playTime2);
				var newhour = parseInt(hour);
				

				if(newhour<newtime||day1>newday1){
					var movieId = this.movieId;
					var movieName = this.movieName;
					var kindName = this.kindName;
					var movieLong = this.movieLong + "分钟";
					var playTime2 = this.playTime2;
					var ticket = "余" + (105 - parseInt(this.ticketNum + "")) + "/105总";
					var languageName = this.languageName;
					var editionName = this.editionName;
					var playPrice = "￥" + this.playPrice + "元";
					var playId = this.playId;
					var rowspan = this.rowspan;
					
					var a1 = "<a onclick=\"showDetails(" + movieId + ")\">" + movieName + "</a>";
					var a2 = "<a onclick=\"buyTicket(" + playId + ")\">我要购票</a>";
					
					var tds = [];
					
					if(rowspan)
					{
						tds.push("<th rowspan=" + rowspan + ">" + a1 + "<br/>" + kindName + "<br/>" + movieLong + "</th>");
						
						preMovieName = movieName;
					}
					else
					{
						if(preMovieName != movieName)
						{
							tds.push("<th>" + a1 + "<br/>" + kindName + "<br/>" + movieLong + "</th>");
						}
					}
					
					
					tds.push("<td>" + playTime2 + "</td>");
					tds.push("<td>" + ticket + "</td>");
					tds.push("<td>" + languageName + "</td>");
					tds.push("<td>" + editionName + "</td>");
					tds.push("<td>" + playPrice + "</td>");
					tds.push("<td>" + a2 + "</td>");
					
					var tr = "<tr>" + tds.join("") + "</tr>";
	
					trs.push(tr);
				}
			});
			
			$("#divTable" + tabIndex + " table").append(trs.join(""));
			
			$("#divTable" + tabIndex + " table").show();
			$("#div_play_msg" + tabIndex).hide();
		}
		else
		{
			$("#div_play_msg" + tabIndex).show();
			$("#divTable" + tabIndex + " table").hide();
		}
	}, null);
}

/************************影片************************/
setMovieCss = function(obj)
{
	var id = obj.id;
	movie_a[id] = !movie_a[id];
	var flag = movie_a[id];

	if(flag)
	{
		setSelectedCss(obj);

		if("movie0" == id)
		{
			setNotSelectedCss($("#spanMovie" + tabIndex + " a:gt(0)"));
			
			for(var key in movie_a)
			{
				if("movie0" != key)
				{
					movie_a[key] = false;
				}
			}
		}
		else
		{
			setNotSelectedCss($("#movie0"));
			
			movie_a["movie0"] = false;
		}
	}
	else
	{
		setNotSelectedCss(obj);
	}
	
	var arr = [];
	
	for(var key in movie_a)
	{
		if(movie_a[key])
		{
			var val = $("#" + key).attr("vals");
			arr.push(val);
		}
	}
	
	args.play = arr.join(",");
	
	queryTime();
}

/************************时间************************/
setTimeCss = function(obj, key)
{
	time_a[key] = !time_a[key];
	var flag = time_a[key];

	if(flag)
	{
		setSelectedCss(obj);

		if("all" == key)
		{
			setNotSelectedCss($("#div_time" + tabIndex + " a:gt(0)"));
			
			time_a.am = false;
			time_a.pm = false;
			time_a.night = false;
		}
		else
		{
			setNotSelectedCss($("#div_time" + tabIndex + " a:eq(0)"));
			
			time_a.all = false;
		}
	}
	else
	{
		setNotSelectedCss(obj);
	}

	var arr = [];
	
	if(time_a.all)
	{
		arr.push("am");
		arr.push("pm");
		arr.push("night");
	}
	
	if(time_a.am)
	{
		arr.push("am");
	}
	
	if(time_a.pm)
	{
		arr.push("pm");
	}
	
	if(time_a.night)
	{
		arr.push("night");
	}
	
	args.time = arr.join("");
	
	queryTime();
}

/************************版本************************/
setEditionCss = function(obj, key)
{
	edition_a[key] = !edition_a[key];
	var flag = edition_a[key];

	if(flag)
	{
		setSelectedCss(obj);

		if("all" == key)
		{
			setNotSelectedCss($("#div_edition" + tabIndex + " a:gt(0)"));
			
			edition_a.imax = false;
			edition_a.d2 = false;
			edition_a.d3 = false;
		}
		else
		{
			setNotSelectedCss($("#div_edition" + tabIndex + " a:eq(0)"));
			
			edition_a.all = false;
		}
	}
	else
	{
		setNotSelectedCss(obj);
	}

	var arr = [];
	
	if(edition_a.all)
	{
		arr.push("1");
		arr.push("2");
		arr.push("3");
	}
	
	if(edition_a.imax)
	{
		arr.push("1");
	}
	
	if(edition_a.d2)
	{
		arr.push("2");
	}
	
	if(edition_a.d3)
	{
		arr.push("3");
	}
	
	args.edition = arr.join(",");
	
	queryTime();
}

setSelectedCss = function(obj)
{
	$(obj).css("backgroundColor", "#3399FF");
	$(obj).css("color", "white");
}

setNotSelectedCss = function(obj)
{
	$(obj).css("backgroundColor", "");
	$(obj).css("color", "#3399FF");
}

resetData = function()
{
	for(var key in args)
	{
		delete args[key];
	}
	
	for(var key in movie_a)
	{
		if("movie0" == key)
		{
			movie_a[key] = false;
		}
		else
		{
			delete movie_a[key];
		}
	}
	
	for(var key in time_a)
	{
		time_a[key] = false;
	}
	
	for(var key in edition_a)
	{
		edition_a[key] = false;
	}
}

resetCss = function()
{
	setNotSelectedCss($("#spanMovie" + tabIndex + " a"));
	setNotSelectedCss($("#div_time" + tabIndex + " a"));
	setNotSelectedCss($("#div_edition" + tabIndex + " a"));
}

// 影片相关详情
showDetails = function(movieId)
{
	window.location = "singleMovie?movieId=" + movieId;
}

buyTicket = function(playId)
{
	var memberId = $("#hdMemberId").val();

	var json = {memberId : memberId, playId : playId};

	if(!memberId)
	{
		window.location = "login.jsp?json=" + $.toJSON(json);
		return;
	}
	
	window.location = "seat.jsp?json=" + $.toJSON(json);
}